<template>
  <div class="chat-msg" :style="chatMsgStyle">
    <div class="avatar-wrap" :style="avatarWrapStyle">
      <van-image :src="avatar" radius="8" class="avatar" />
    </div>
    <slot name="chat">
      <div class="chat-info">
        <div class="user-name" :v-if="name">{{name}}</div>
        <div
          :class="['chat-box',{'bg-green':align==='right','bg-white':align==='left'}]"
          :style="{backgroundColor:background}"
        >
          <div
            class="chat-arrow arrow-r"
            :style="{borderleftColor:background}"
            v-if="align === 'right'"></div>
          <div
            class="chat-arrow arrow-l"
            :style="{borderRightColor:background}"
            v-else></div>
          <div class="content">
            <slot name="chatcontent"></slot>
          </div>
        </div>
      </div>
    </slot>
  </div>
</template>

<script>
export default {
  name: 'BaseChatMsgItem',
  props: {
    avatar: String,
    name: String,
    align: {
      type: String,
      validator: value => {
        return ['left', 'right'].includes(value)
      }
    },
    background: String
  },
  data() {
    return {}
  },
  computed: {
    chatMsgStyle() {
      return { flexDirection: this.align === 'right' ? 'row-reverse' : 'row' }
    },
    avatarWrapStyle() {
      return this.align === 'right' ? { marginLeft: '10px' } : { marginRight: '10px' }
    }
  },
  mounted() {},
  destroyed() {},
  methods: {}
}
</script>

<style lang="scss" scoped>
$mr: 10px;
$chat-box-pad: 12px;
$avatar-size: 50px;
$msg-bg-green: #97eb62;
$msg-bg-white: #fdfdfd;
.bg-green{
  background-color: $msg-bg-green;
}
.bg-white{
  background-color: $msg-bg-white;
}
.chat-msg {
  margin-bottom: 10px;
  display: flex;
  .avatar-wrap {
    width: $avatar-size;
    height: $avatar-size;
    .avatar {
      width: $avatar-size;
      height: $avatar-size;
    }
  }
  .chat-info {
    .user-name{
      font-size: $font-size-md;
      color: $gray-7;
    }
    .chat-box {
      position: relative;
      min-height: $avatar-size;
      padding: $chat-box-pad;
      line-height: $avatar-size - 2 * $chat-box-pad;
      font-size: 16px;
      border-radius: 4px;
      box-sizing: border-box;
      color: #000;
      max-width: calc(100vw - 130px);
      font-weight: 500;
      .chat-arrow{
        position: absolute;
        width: 0;
        height: 0;
        border-color: transparent;
        border-style: solid;
        border-width: 6px;
        top: 19px;
      }
      .arrow-l {
        border-right-color: $msg-bg-white;
        border-left-width: 0;
        left: -5px;
      }
      .arrow-r {
        border-left-color: $msg-bg-green;
        border-right-width: 0;
        right: -5px;
      }
    }
  }
}
</style>
